﻿using Microsoft.Phone.Controls;
using System;
using Microsoft.Phone.Shell;
using System.Windows;
using eLearning.Model;
using eLearning.Util;
using System.Net;
using Newtonsoft.Json;
using System.Text;
using System.Windows.Controls;

namespace eLearning.MahasiswaView
{
    public partial class ResourcesPage : PhoneApplicationPage
    {
        private Perkuliahan kuliah;
        private Resources selectedResources;
        
        public ResourcesPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(DataContextPage_Loaded);
        }

        private void DataContextPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (PhoneApplicationService.Current.State.ContainsKey(Config.KULIAH))
            {
                kuliah = (Perkuliahan)PhoneApplicationService.Current.State[Config.KULIAH];
            }
            if (kuliah == null)
            {
                kuliah = new Perkuliahan();
                kuliah.Mhs = (Mahasiswa)SavedUser.Instance.User.ConvertUser();
            }

            LoadResourcesList();
        }

        private void LoadResourcesList()
        {
            listResources.ItemsSource = null;
            selectedResources = null;

            string url = string.Format("{0}&{1}", Config.Instance.RESOURCES_SERVICE_URL() + Config.VIEW_ACTION_KEY + "ResourcesMhs", Guid.NewGuid().ToString());
            string json = JsonConvert.SerializeObject(kuliah);

            Uri uri = new Uri(url, UriKind.Absolute);
            WebClient webClient = new WebClient();

            webClient.Encoding = Encoding.UTF8;
            webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
            webClient.Headers[HttpRequestHeader.ContentLength] = json.Length.ToString();

            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(listResources_UploadStringCompleted);
            try
            {
                webClient.UploadStringAsync(uri, "POST", json);
            }
            catch (Exception) { }
        }

        private void listResources_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            try
            {
                ResourcesList list = JsonConvert.DeserializeObject<ResourcesList>(e.Result);
                if (list.Count > 0)
                    listResources.ItemsSource = list;
                else
                    listResources.ItemsSource = null;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void backButton_Click(object sender, System.EventArgs e)
        {
            NavigationService.GoBack();
        }

        private void listResources_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                selectedResources = (Resources)e.AddedItems[0];
                ((ListBox)sender).SelectedIndex = -1;

                if (selectedResources != null)
                {
                    pivotResources.SelectedIndex = 1;
                    textBoxCreated.Text = selectedResources.Created.ToString("dd/MM/yyyy");
                    textBoxJudul.Text = selectedResources.Judul;
                    textBoxLink.Text = selectedResources.Link;
                }
            }
        }

        private void pivotResources_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (selectedResources == null)
            {
                textBoxCreated.Text = "";
                textBoxJudul.Text = "";
                textBoxLink.Text = "";
            }
        }

        private void TextBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                var task = new Microsoft.Phone.Tasks.WebBrowserTask
                {
                    Uri = new Uri(textBoxLink.Text.Contains("http://") ? textBoxLink.Text : "http://" + textBoxLink.Text)
                };

                task.Show();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void refreshButton_Click(object sender, EventArgs e)
        {
            LoadResourcesList();
        }
    }
}